rm(list=ls())
setwd("")

#reading data
library(foreign)
data <- read.csv2("WhoRevolts_dataset_v01.csv")
names(data)


#creating table
tab <- matrix(NA,18,3)
tab[1,1] <- sum(data$urban_dominate,na.rm=T)
tab[2,1] <- sum(data$atleast_urban,na.rm=T)
tab[3,1] <- sum(data$originate_urban,na.rm=T)
tab[4,1] <- sum(data$indwork_dominate,na.rm=T)
tab[5,1] <- sum(data$atleast_indwork,na.rm=T)
tab[6,1] <- sum(data$originate_indwork,na.rm=T)
tab[7,1] <- sum(data$peasant_dominate,na.rm=T)
tab[8,1] <- sum(data$atleast_peasant,na.rm=T)
tab[9,1] <- sum(data$originate_peasant,na.rm=T)
tab[10,1] <- sum(data$pubemp_dominate,na.rm=T)
tab[11,1] <- sum(data$atleast_pubemp,na.rm=T)
tab[12,1] <- sum(data$originate_pubemp,na.rm=T)
tab[13,1] <- sum(data$milemp_dominate,na.rm=T)
tab[14,1] <- sum(data$atleast_milemp,na.rm=T)
tab[15,1] <- sum(data$originate_milemp,na.rm=T)
tab[16,1] <- sum(data$relethnic_dominate,na.rm=T)
tab[17,1] <- sum(data$atleast_relethnic,na.rm=T)
tab[18,1] <- sum(data$originate_relethnic,na.rm=T)

tab[1,2] <- sum(!is.na(data$urban_dominate))
tab[2,2] <- sum(!is.na(data$atleast_urban))
tab[3,2] <- sum(!is.na(data$originate_urban))
tab[4,2] <- sum(!is.na(data$indwork_dominate))
tab[5,2] <- sum(!is.na(data$atleast_indwork))
tab[6,2] <- sum(!is.na(data$originate_indwork))
tab[7,2] <- sum(!is.na(data$peasant_dominate))
tab[8,2] <- sum(!is.na(data$atleast_peasant))
tab[9,2] <- sum(!is.na(data$originate_peasant))
tab[10,2] <- sum(!is.na(data$pubemp_dominate))
tab[11,2] <- sum(!is.na(data$atleast_pubemp))
tab[12,2] <- sum(!is.na(data$originate_pubemp))
tab[13,2] <- sum(!is.na(data$milemp_dominate))
tab[14,2] <- sum(!is.na(data$atleast_milemp))
tab[15,2] <- sum(!is.na(data$originate_milemp))
tab[16,2] <- sum(!is.na(data$relethnic_dominate))
tab[17,2] <- sum(!is.na(data$atleast_relethnic))
tab[18,2] <- sum(!is.na(data$originate_relethnic))




tab[1,3] <- sum(data$urban_dominate,na.rm=T)/sum(!is.na(data$urban_dominate))
tab[2,3] <- sum(data$atleast_urban,na.rm=T)/sum(!is.na(data$atleast_urban))
tab[3,3] <- sum(data$originate_urban,na.rm=T)/sum(!is.na(data$originate_urban))
tab[4,3] <- sum(data$indwork_dominate,na.rm=T)/sum(!is.na(data$indwork_dominate))
tab[5,3] <-  sum(data$atleast_indwork,na.rm=T)/sum(!is.na(data$atleast_indwork))
tab[6,3] <- sum(data$originate_indwork,na.rm=T)/sum(!is.na(data$originate_indwork))
tab[7,3] <- sum(data$peasant_dominate,na.rm=T)/sum(!is.na(data$peasant_dominate))
tab[8,3] <-  sum(data$atleast_peasant,na.rm=T)/sum(!is.na(data$atleast_peasant))
tab[9,3] <- sum(data$originate_peasant,na.rm=T)/sum(!is.na(data$originate_peasant))
tab[10,3] <- sum(data$pubemp_dominate,na.rm=T)/sum(!is.na(data$pubemp_dominate))
tab[11,3] <-  sum(data$atleast_pubemp,na.rm=T)/sum(!is.na(data$atleast_pubemp))
tab[12,3] <- sum(data$originate_pubemp,na.rm=T)/sum(!is.na(data$originate_pubemp))
tab[13,3] <- sum(data$milemp_dominate,na.rm=T)/sum(!is.na(data$milemp_dominate))
tab[14,3] <-  sum(data$atleast_milemp,na.rm=T)/sum(!is.na(data$atleast_milemp))
tab[15,3] <- sum(data$originate_milemp,na.rm=T)/sum(!is.na(data$originate_milemp))

tab[16,3] <- sum(data$relethnic_dominate,na.rm=T)/sum(!is.na(data$relethnic_dominate))
tab[17,3] <-  sum(data$atleast_relethnic,na.rm=T)/sum(!is.na(data$atleast_relethnic))
tab[18,3] <- sum(data$originate_relethnic,na.rm=T)/sum(!is.na(data$originate_relethnic))




colnames(tab) <- c("Sum", "N", "Share")
rownames(tab) <- c("Middle class dominates", "Middle class participates", "Originated among middle class", "Workers dominate", "Workers participate", "Originated among workers",
                   "Peasants dominate", "Peasants participate", "Originated among peasants", "Public employees dominate", "Public employees participate", "Originate among pub. employees",
                   "Military dominates", "Military participates", "Originated among military", "Religious/ethnic group dominate",
                   "Religious/ethnic group participated", "Originated among religious/ethnic groups")




library(xtable)
xtable(print( tab), digits=2)
